<template>
  <div class="container">
    <div class="search-box">
      <el-input placeholder="请输入内容" v-model="keyWord" class="input-box"></el-input>
      <el-button @click="clearSearch">清除</el-button>
      <el-button type="primary" @click="search">搜索</el-button>
    </div>
    <el-table border :data="tableData" style="width: 100%">
      <el-table-column type="index" label="任务编号" width="80" :index="indexAdd">
      </el-table-column>
      <el-table-column prop="name" label="任务名称"> </el-table-column>
      <el-table-column prop="des" label="任务描述"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template slot-scope="scope">
          <el-button size="mini" style="margin-right: 10px" @click="editBook(scope.row.id)"
            >编辑</el-button
          >
          <el-popconfirm
            confirmButtonText="确定"
            cancelButtonText="不用了"
            icon="el-icon-info"
            iconColor="red"
            title="确定删除任务吗？"
            @confirm="delBook(scope.row.id)"
          >
            <template #reference>
              <el-button size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="pageContainer">
      <el-pagination
        layout="prev, pager, next"
        :total="total"
        :page-size="params._limit"
        @current-change="pageChange"
      />

      <el-button size="small" plain @click="openShowDialog">+ 新增</el-button>
    </div>
    <!-- 新增窗口 -->
    <el-dialog title="提示" :visible="dialogVisible" @close="closeDialog" width="30%">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="任务名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="任务描述" prop="des">
          <el-input v-model="form.des"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="confirm1">确定</el-button>
          <el-button @click="closeDialog">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  fetchBookList,
  fetchAddBook,
  fetchBookDetail,
  fetchUpdateBook,
  fetchDelBook,
  fetchSearchBook
} from '@/api/book.js'
import { v4 as uuidv4 } from 'uuid'
export default {
  data() {
    return {
      tableData: [],
      params: {
        _page: 1, // 当前第几页
        _limit: 5 // 每页条数
      },
      total: 0,
      dialogVisible: false,
      form: {
        id: null,
        name: '',
        des: ''
      },
      keyWord: '',
      rules: {
        name: [
          { required: true, message: '请输入任务名称', trigger: 'blur' },
          { min: 2, max: 6, message: '长度在 2 到 6 个字符', trigger: 'blur' }
        ],
        des: [
          { required: true, message: '请输入任务描述', trigger: 'blur' },
          { min: 2, max: 6, message: '长度在 2 到 6 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    // 获取列表
    async getList() {
      const res = await fetchBookList(this.params)
      this.tableData = res.data
      this.total = Number(res.headers['x-total-count'])
    },
    indexAdd(index) {
      let curPage = this.params._page
      let curPageSize = this.params._limit
      return index + 1 + (curPage - 1) * curPageSize
    },
    // 分页
    pageChange(page) {
      this.params._page = page
      this.getList()
    },
    // 打开新增窗口
    openShowDialog() {
      this.dialogVisible = true
    },
    // 关闭新增窗口
    closeDialog() {
      this.dialogVisible = false
    },
    // 新增窗口
    async confirm1() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          if (this.form.id) {
            await fetchUpdateBook(this.form)
          } else {
            this.form.id = Number(uuidv4())
            await fetchAddBook(this.form)
          }
          this.$message.success(`${this.form.id ? '编辑' : '添加'}部门成功`)
          this.reset()
          this.getList()
        }
      })
    },
    reset() {
      this.dialogVisible = false
      this.form = {
        id: null,
        name: '',
        des: ''
      }
    },
    async editBook(id) {
      console.log(id)
      this.dialogVisible = true
      const res = await fetchBookDetail(id)
      this.form = res.data
    },

    // 删除
    async delBook(id) {
      console.log(id)
      await fetchDelBook(id)
      this.$message.success('删除成功')
      this.getList()
    },
    // 搜索
    async search() {
      if (this.keyWord) {
        const res = await fetchSearchBook(this.keyWord)
        console.log(res)
        this.tableData = res.data

        this.total = res.data.length
      } else {
        this.$message.warning('内容不能为空')
      }
    },
    // 清除
    clearSearch() {
      this.keyWord = ''
      this.getList()
    }
  }
}
</script>

<style scoped lang="less">
.container {
  width: 800px;
  margin: 0 auto;
}
.search-box {
  display: flex;
  margin-bottom: 10px;
  .input-box {
    margin-right: 20px;
  }
}
.pageContainer {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
}
</style>
